<template>
  <div class="divBox relative">
    <el-card class="box-card" style="min-height: 80vh">
      <div slot="header" class="clearfix" style="margin-bottom: -20px ;">
        <div class="container">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>
<!--            <el-form-item>-->
<!--              <el-input v-model="queryForm.id" placeholder="请输入id" clearable @keyup.enter.native="search()"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item>
              <el-select v-model="queryForm.source" placeholder="请选择app渠道号" class="width100" filterable clearable>
                <el-option
                  v-for="item in sourceList"
                  :label="item.source"
                  :key="item.source"
                  :value="item.source">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="queryForm.time"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions">
              </el-date-picker>

            </el-form-item>
            <el-form-item>
              <el-button @click="search()">查询</el-button>
<!--              <el-button v-if="checkPermi(['userreport:index:save'])" type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
<!--              <el-button v-if="checkPermi(['userreport:index:delete'])"  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量</el-button>-->
<!--            -->
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;" size="mini" :highlight-current-row="true" :header-cell-style=" {fontWeight:'bold'}">
        <el-table-column type="selection" header-align="center" align="center" width="50"/>
<!--           <el-table-column prop="id" label="id" :show-overflow-tooltip="true"></el-table-column>-->
           <el-table-column prop="date" label="日期" :show-overflow-tooltip="true"></el-table-column>
           <el-table-column prop="source" label="渠道号" :show-overflow-tooltip="true"></el-table-column>
           <el-table-column prop="registcount" label="激活数据" :show-overflow-tooltip="true"></el-table-column>
<!--           <el-table-column prop="activecount" label="用户活跃数量" :show-overflow-tooltip="true"></el-table-column>-->
           <el-table-column prop="createtime" label="创建时间" :show-overflow-tooltip="true"></el-table-column>

<!--        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">-->
<!--          <template slot-scope="scope">-->
<!--            <el-button type="text"  v-if="checkPermi(['userreport:index:update'])" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
<!--            <el-button type="text"  v-if="checkPermi(['userreport:index:delete'])" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
      <el-pagination
              @size-change="sizeChangeHandle"
              @current-change="currentChangeHandle"
              :current-page="tableForm.pageIndex"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="tableForm.pageSize"
              :total="tableForm.total"
              layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-card>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './userreport-add-and-update.vue'
  import api from '@/api/api'
  import {checkPermi} from "@/utils/permission";
  import moment from 'moment'

  export default {
    components: {
      AddOrUpdate
    },
    data () {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        sourceList: [],
        dataList: [],
        queryForm: {
          id: '',
          source:'',
          time:''
        },
        tableForm:{
          pageIndex: 1,
          pageSize: 10,
          total: 0,
        },
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    mounted () {
      this.search()
      this.getSourcelist()
    },
    methods: {
      checkPermi,
      // 每页数
      sizeChangeHandle (val) {
        this.tableForm.pageSize = val
        this.tableForm.pageIndex = 1
        this.search()
      },
      // 当前页
      currentChangeHandle (val) {
        this.tableForm.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      change(row){
        let postData={id:row.id}
        postData.status=row.status==1?0:1;
        api.post('rest/vabc/userreport/updateById',postData).then(res => {
          this.$message.success("操作成功")
          row.status=postData.status
          // this.getDataList()
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`您确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let postData={ids:ids.join(',')};
          api.post('rest/vabc/userreport/delete',postData).then(res => {
            this.$message.success("删除成功")
            this.getDataList()
          })
        })
      },
      search(){
        this.tableForm.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.dataListLoading = true
        let postData={
          'order':'desc',
          'column':'id'
        }
        Object.assign(postData,this.queryForm, this.tableForm)
        if (postData.time&&postData.time.length > 0) {
          postData.date_begin=moment(postData.time[0]).format('YYYY-MM-DD')
          postData.date_end=moment(postData.time[1]).format('YYYY-MM-DD')
        }
        //postData.name=api.getLikeValue( postData.name);
        api.post('rest/vabc/userreport/list',postData).then(res => {
          this.dataList=res.datalist
          this.tableForm.total=res.total
        }).finally(() => {
          this.dataListLoading = false
        });
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
        })
      },
      getSourcelist () {
        let postData={
        }
        Object.assign(postData,this.queryForm, this.tableForm)
        //postData.name=api.getLikeValue( postData.name);
        api.post('rest/vabc/userEntity/getSourcelist',postData).then(res => {
          this.sourceList=res.datalist
        }).finally(() => {
        });
      },
    }
  }
</script>
